package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;

import java.io.IOException;
import java.util.Scanner;

public class LoginPage extends Utils {
    //登录页URL
public static String url = "https://tieba.baidu.com/";
    public LoginPage() {
        super(url);
    }
    public void loginPageRight(){
        //通过查看页面元素查看网页是否加载.成功

        //选取 百度贴吧首-导航栏-百度首页的selector
         driver.findElement(By.cssSelector("#com_userbar > ul > li.u_bdhome > div > a"));
         //点击登录显示的弹窗 X 号
         driver.findElement(By.cssSelector("#tiebaCustomPassLogin > div.tieba-login-wrapper > span")).click();
    }




    //登录成功
    public void LoginSuc() throws InterruptedException, IOException {
        //检查登录功能
        //点击登录按钮
        driver.findElement(By.cssSelector("#com_userbar > ul > li.u_login > div > a")).click();
        //点击用户名登录
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__footerULoginBtn")).click();
        //输入用户名
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__userName")).sendKeys("18654503508");
        //输入密码
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__password")).sendKeys("bc1d59769a13");
        //点击登录
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__submit")).click();

        //进入手机号验证
        Thread.sleep(5000);
        driver.findElement(By.cssSelector("#goToVerify")).click();
        //提示输入验证码
        System.out.println("请输入验证码");
        Scanner sc = new Scanner(System.in);

        String str = sc.next();
        //强制等待2秒
        Thread.sleep(2000);
        System.out.println("输入成功,正在执行登录……");
        //向网页输入验证码
        driver.findElement(By.cssSelector("#passAuthVcode")).sendKeys(str);
        //点击下一步
        driver.findElement(By.cssSelector("#passAuthSubmitCode")).click();



        //检查是否登录成功  通过导航栏-更多-退出  检查是否有退出元素
        //强制等待1秒
        Thread.sleep(1000);
       try{
           driver.findElement(By.cssSelector("#com_userbar > ul > li.u_setting > div.u_ddl > div > div.u_ddl_con_top > ul > li.u_logout > a"));
           System.out.println("登录成功");
       }catch (NoSuchElementException e){
           System.out.println("登录失败");
       }

       //屏幕截图
        Thread.sleep(1000);
        getPicture(getClass().getName());
        System.out.println("保存截图成功");

    }


    //登录失败
    public void LoginFail() throws IOException, InterruptedException {
        //检查登录功能
        //点击登录按钮
        driver.findElement(By.cssSelector("#com_userbar > ul > li.u_login > div > a")).click();
        //点击用户名登录
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__footerULoginBtn")).click();
        //输入用户名
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__userName")).sendKeys("18654523508");
        //输入密码
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__password")).sendKeys("bc1d577769a13");
        //点击登录
        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__submit")).click();

        driver.findElement(By.cssSelector("#TANGRAM__PSP_23__error"));

        try {
            driver.findElement(By.cssSelector("#TANGRAM__PSP_23__error"));
            System.out.println("登录失败.账号或密码错误");
        }catch (NoSuchElementException e){

        }
        //屏幕截图
        Thread.sleep(1000);
        getPicture(getClass().getName());
        System.out.println("保存截图成功");

    }
}
